<template>
  <el-dialog
    title="修改链接"
    class="dialog-header"
    width="680px"
    :visible.sync="isOpen"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="closeHandler">
    <div class="dialog-body">
      <el-form label-position="top" label-width="80px" :model="formData" :rules="rules">
        <el-form-item label="链接标签" prop='label'>
          <el-select v-model="formData.label" placeholder="请选择">
            <el-option v-for="item in labelOptions" :key="item" :label="labelText(item)" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeHandler">取 消</el-button>
      <el-button type="primary" @click="closeHandler('confirm')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import jsUtil from '@/utils/helper/jsUtil'
import { LabelText } from '@/model/edge.model'

export default {
  name: 'EdgeDetailsPanel',
  data() {
    return {
      LabelText,
      isOpen: false,
      labelOptions: [],
      formData: {
        label: ''
      },
      rules: {
        label: [{ required: true, message: '链接标签不能为空', trigger: 'blur' }]
      }
    }
  },
  props: {
    edge: {
      required: true
    },
    visible: {
      type: Boolean,
      required: true
    }
  },
  watch: {
    visible: {
      handler(val) {
        this.isOpen = val
      },
      immediate: true
    },
    edge(val) {
      if (val && !jsUtil.isVoid(val)) {
        const edgeSourceNode = val.getSourceNode()
        const nodeData = edgeSourceNode.getData()
        this.labelOptions = jsUtil.deepClone(nodeData.additionalInfo?.relationTypes || [])
        // 如果标签是中文，则需要转换成对应的key
        const label = val.labels[0]?.attrs.label.text
        this.formData.label = LabelText[label] || label
      }
    }
  },
  methods: {
    labelText(item) {
      const sourceNodeData = this.edge.getSourceNode().getData()
      if (['SceneLibFrontConditionNode', 'SceneLibAutoTouchNode', 'SceneLibAutoExitNode', 'SceneLibManualTouchNode', 'SceneLibManualExitNode', 'AdvancedLibStateChangeNode'].includes(sourceNodeData.additionalInfo.component)) {
        return '成立'
      } else {
        return LabelText[item] || item
      }
    },
    closeHandler(update = '') {
      this.isOpen = false
      if (update === 'confirm' && this.labelOptions?.length) {
        const sourceNodeData = this.edge.getSourceNode().getData()
        // 如果是前置条件节点、自动触发节点、自动退出节点、主动触发节点、主动退出节点、状态变化节点，则label为成立
        if (['SceneLibFrontConditionNode', 'SceneLibAutoTouchNode', 'SceneLibAutoExitNode', 'SceneLibManualTouchNode', 'SceneLibManualExitNode', 'AdvancedLibStateChangeNode'].includes(sourceNodeData.additionalInfo.component)) {
          this.edge.setLabels('成立')
        } else {
          // 如果标签是英文则转换成中文
          this.edge.setLabels(LabelText[this.formData.label] || this.formData.label)
        }
      }
      this.formData.label = ''
      this.labelOptions = []
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
